﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Practice.PH4_2104A.Shopping.SevenLayerFramework.Domin;

namespace Practice.API.Controllers.order
{
    /// <summary>
    /// 订单2控制器
    /// </summary>
    [Route("order2")]
    [ApiController]
    public class Order2Controller : ControllerBase
    {
        #region 模拟数据

        static List<OrderMasterModel> _masters; //订单信息
        static List<OrderDetailModel> _details; //订单明细信息
        /// <summary>
        /// 
        /// </summary>
        public Order2Controller()
        {
            if (_masters == null || _details == null)
            {
                _masters = new List<OrderMasterModel>();
                _details = new List<OrderDetailModel>();

                //int b = new Random().Next(1, 10);
                for (int k = 0; k < 5; k++)
                {
                    //模拟生成的订单主表
                    var master = new OrderMasterModel
                    {
                        Id = Guid.NewGuid(),
                        OrderAmount = new Random().Next(100, 1000),
                        PayType = Domain.Shared.OrderPayTypeEnum.Alipay,
                        OrderState = Enum.Parse<Domain.Shared.OrderStateEnum>(new Random().Next(1,6).ToString())
                };
                    _masters.Add(master);
                    //模拟生成订单明细表
                    int a = new Random().Next(1, 5);
                    for (int i = 0; i < a; i++)
                    {
                        _details.Add(new OrderDetailModel
                        {
                            Id = Guid.NewGuid(),
                            OrderNo = master.OrderNo,
                            SkuId = Guid.NewGuid(),
                            Price = new Random().Next(1, 100),
                            Number = new Random().Next(1, 10),
                            Image = $"{new Random().Next(1, 5)}.jpg",
                            Name = $"随机商品{new Random().Next(1, 100)}名称"
                        });
                    }
                }
            }
        }
        #endregion

        /// <summary>
        /// 订单查询
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult Get(string? orderNo = "", string? productName = "",int? state=-1)
        {
            //根据产品名称查订单号，并去重
            var os = _details.Where(x => x.Name.Contains(productName ?? ""))
                        .DistinctBy(x=>x.OrderNo).Select(x => x.OrderNo);         

            //根据订单号查询
            var q = from a in os
                    join b in _masters on a equals b.OrderNo
                    where a.Contains(orderNo??"")
                    select new
                    {
                        Master = b,
                        Products = _details.Where(x => x.OrderNo.Equals(a)).ToList()
                    };

            //根据订单状态查询
            if(state >= 0)
            {
                q = q.Where(x => (int)x.Master.OrderState == state);
            }

            return Ok(q);
        }

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="orderNos">订单号</param>
        /// <returns></returns>
        [HttpDelete]
        public IActionResult Delete(List<string> orderNos)
        {
            orderNos.ForEach(x =>
            {
                var m = _masters.Where(p => p.OrderNo.Equals(x)).FirstOrDefault();
                if(m != null) _masters.Remove(m);
            });

            return Ok();
        }

    }

    #region 模拟数据
    public class OrderData
    {
        public OrderMasterModel Master { get; set; }
        public List<OrderDetailModel> Products { get; set; }
    }
    #endregion
}
